<template>
  <!-- 部门添加对话框 -->
  <el-dialog title="部门添加" :visible="dialogvisible" @close="onClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small">确定</el-button>
        <el-button size="small" @click="onClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 查询企业的部门列表请求
import { getDapartment } from '@/api/departments'
export default {
  filters: {},
  components: {},
  props: {
    // 控制部门添加对话框
    dialogvisible: {
      type: Boolean,
      required: true
    },
    // 选择部门数据
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    // 自定义部门验证规则
    const isRepeatDeptsName = (rules, value, callback) => {
      if (!this.node.children) return callback()
      const valie = this.node.children.some(item => item.name === value)
      if (valie) {
        callback(new Error('部门已存在'))
      }
      callback()
    }
    // 自定义编码验证规则
    const isRepeatCode = async(rules, value, callback) => {
      const res = await getDapartment()
      const valie = res.depts.some(item => item.code === value)
      if (valie) {
        callback(new Error('编码已存在'))
      }
      callback()
    }
    return {
      // 表单数据
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: isRepeatDeptsName, trigger: 'blur' }
        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: isRepeatCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 点击部门添加
    onClose() {
      this.$emit('update:dialogvisible', false)
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped lang='less'>

</style>
